/*****************************************************************************
 * boer
 *****************************************************************************
 * WeixinController.java
 *----------------------------------------------------------------------------
 * 版权HeartArea所有，联系ding_cuiping@163.com
 *----------------------------------------------------------------------------
 * Version    Date       By            Comment
 * 1.0        2014-3-20    Poppy       新建
 *****************************************************************************/
package net.heartarea.boer.apps.weixin.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.heartarea.boer.apps.weixin.Router;
import net.heartarea.boer.apps.weixin.pojo.CheckBean;
import net.heartarea.boer.util.EncryptUtil;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eightspace.server.common.log.LogManagment;

/**
 * FIXME 此处填写类的描述信息
 * 
 * @author Poppy
 * @date 2014-3-20
 * @since 1.0
 */
@Controller("weixinController")
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class weixinController {

    /**
     * 日志对象
     */
    private static LogManagment log = LogManagment.getLogger(weixinController.class);

    /**
     * service
     */
    @Resource(name = "router")
    private Router router;



    @RequestMapping("/vxin")
    public void router(HttpServletRequest request, HttpServletResponse response) {
        String result = "";
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            result = router.msg(request);
            System.out.println(result);
            response.getWriter().write(result);
        } catch (UnsupportedEncodingException e1) {
        	log.error("", e1);
        } catch (ServletException e) {
            log.error("", e);
        } catch (IOException e) {
            log.error("", e);
        }
    }



    /**
     * 
     * 校验接口
     * 
     * @author Frank
     * @throws IOException
     * @date 2014-3-8
     * @since 1.0
     */
    @RequestMapping("/vxin1234")
    @ResponseBody
    public void check(CheckBean checkBean, HttpServletResponse response, HttpServletRequest request) throws IOException {
        String result = "";
        String token = "heartareatoken";
        String[] stra = new String[] { checkBean.getNonce(), checkBean.getTimestamp(), token };
        Arrays.sort(stra);
        String tmpStr = "";
        for (String s : stra) {
            tmpStr += s;
        }
        tmpStr = EncryptUtil.encode("sha1", tmpStr);
        if (tmpStr != null && tmpStr.equals(checkBean.getSignature())) {
            result = checkBean.getEchostr();
        }
        response.getWriter().write(result);
    }

}
